# linux脚本
cd /allen-home/RuoYi-Vue
AppName=ruoyi-admin/target/ruoyi-admin.jar

# JVM参数
JVM_OPTS="-Dspring.profiles.active=druid -Dname=${AppName##*/} -Duser.timezone=Asia/Shanghai -Xms512m -Xmx1024m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps  -XX:+PrintGCDetails -XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC"

query(){
  PID=`ps -ef |grep java|grep $AppName|grep -v grep|awk '{print $2}'`
}

start(){
  query
  if [ x"$PID" != x"" ]; then
    echo "$AppName is running..."
  else
    nohup java $JVM_OPTS -jar $AppName > /dev/null 2>&1 &
    echo "Start $AppName success..."
    echo "logPath >>> tail -f /allen-home/ruoyi/logs/sys-info.log"
    # tail -f /allen-home/ruoyi/logs/sys-info.log
  fi
}

stop(){
  query
  if [ x"$PID" != x"" ]; then
    kill -TERM $PID
    echo "$AppName (pid:$PID) exiting..."
    while [ x"$PID" != x"" ]
    do
      sleep 1
      query
    done
    echo "$AppName exited."
  else
    echo "$AppName already stopped."
  fi
}

restart(){
  echo '重启服务...'
  stop
  sleep 2
  start
}

deploy(){
  echo '部署服务...'
  git pull
  mvn clean package -T 4C -Dmaven.test.skip=true -Dmaven.compile.fork=true -T 4C
  restart
}

function status()
{
  query
  if [[ $PID > 0 ]];then
    echo "$AppName is running..."
  else
    echo "$AppName is not running..."
  fi
}

build(){
  echo '构建ui服务...'
  git pull
  cd ruoyi-ui/
  npm run build:prod
}

if [ "$1" = "" ]
then
  echo 'please input [./run.sh deploy-部署|start-启动|restart-重启|stop-停止|status-查看状态|build-构建ui'
else  
  $1
  echo '执行完毕...'
fi
